package com.yue.control;

import com.yue.bean.UserInfoBean;
import com.yue.modal.UserModel;
import com.yue.utils.MyUtils;
import org.apache.commons.lang3.StringUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet("/UserInfoQueryServlet")
public class UserInfoQueryServlet extends HttpServlet {
    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // 1 获取表单数据
        UserInfoBean userInfoBean = MyUtils.getNewInstance(UserInfoBean.class);
        userInfoBean.setUserName(request.getParameter("userName"));

        userInfoBean.setUserTel(request.getParameter("userTel"));
        // 把从前台页面获取的表单数据的值转换为 日期 Date 类型
        String strRegiseterDate = request.getParameter("userRegisteDate");
        // 把前台页面传递的string类型转换为日期的date类型
        if (StringUtils.isNotEmpty(strRegiseterDate)) {
            userInfoBean.setUserRegisteDate(MyUtils.convertString2Date(strRegiseterDate, "yyyy-MM-dd"));
        }
        // 查询条件 -- 截止日期
        String strRegiseterDate2 = request.getParameter("userRegisteDate2");
        if (StringUtils.isNotEmpty(strRegiseterDate2)) {
            userInfoBean.setUserRegisteDate2(MyUtils.convertString2Date(strRegiseterDate2, "yyyy-MM-dd"));
        }

        // 2 调用model层进行业务逻辑查询处理
        // 调用Model层进行查询操作，将用户数据查询出来，保存到session中
        List<UserInfoBean> lstUsers = MyUtils.getNewInstance(UserModel.class).queryUserInfoByParams(userInfoBean);
        // 在跳转之前，需要将显示在成功页面的数据进行session缓存
        request.getSession().setAttribute("lstUsers", lstUsers);

        // 3 显示查询结果页面
        request.getRequestDispatcher("WEB-INF/viewpages/ userInfoB/UserInfoAddDetail.jsp").forward(request, response);
    }
}
